package com.mason.parser;

import com.mason.EnableOperationLog;
import com.mason.model.OperationLog;
import com.mason.service.IService;
import com.mason.util.ReflectionUtils;
import com.mason.util.SpringUtil;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @author yunfei.shang
 * @title
 * @date 2018/05/24 11:19
 * @descreption 基础解析类
 * 单表编辑时可以直接使用id来查询
 * 如果为多表复杂逻辑，请自行编写具体实现类
 */
public class DefaultContentParse implements IContentParser {
    @Override
    public Object getResult(Map<String,Object> fieldValues, EnableOperationLog enableOperationLog, OperationLog operationLog) {
        Assert.isTrue(fieldValues != null && fieldValues.size() != 0,"未解析到主键信息");
        IService service= null;
        Class cls= enableOperationLog.serviceclass();
        service = (IService) SpringUtil.getBean(cls);
        if(operationLog != null) {
            String originKey = "";
            for (Map.Entry entry: fieldValues.entrySet()) {
                originKey += entry.getValue();
            }
            operationLog.setOriginKey(originKey);
        }
        String queryMethods = enableOperationLog.queryMethodName();
        if(StringUtils.isEmpty(queryMethods)) {
            return service.selectByPrimaryMap(fieldValues);
        } else {
            Object[] objects = new Object[]{fieldValues};
            //反射获取函数
            return ReflectionUtils.invokeMethodByName(service, queryMethods, objects);
        }
    }


}
